/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2025-06-23     N0953692       the first version
 */
/*bsp的初始化*/
#include <bsp.h>


/* 线程控制块 */
static rt_thread_t work_thread  = RT_NULL;
static rt_thread_t keys_thread = RT_NULL;
rt_uint8_t c1 = 0 ;

void bsp_init(void)
{
    led_init();
    keys_init();

}

/* work线程入口函数 */
static void work_thread_entry(void *parameter)
{

    while (1)
    {
        for (int i = 0; i < 3; i++) {
            enum key_event event = get_key_event(i);
            if (event != KEY_EVENT_NONE) {
                rt_kprintf("Key%d: ", i+1);

                switch (event) {
                case KEY_EVENT_CLICK:
                    rt_kprintf("Click\n");
                    if (i == 0 ) {
                        led_control(LED_B_PIN,1);
                    }
                    else if (i == 1) {
                        led_control(LED_G_PIN,1);
                    }
                    else if (i == 2) {
                        rgb_led_control(1, 1, 1);
                    }

                    break;
                case KEY_EVENT_DOUBLE_CLICK:
                    rt_kprintf("Double Click\n");
                    if (i == 0 ) {
                            led_control(LED_B_PIN,0);
                        }
                        else if (i == 1) {
                            led_control(LED_G_PIN,0);
                        }
                        else if (i == 2) {
                            rgb_led_control(0, 0, 0);
                        }
                    break;
                case KEY_EVENT_LONG_PRESS:
                    rt_kprintf("Long Press\n");
                    if (i=2) {
                        c1 = c1 +1  ;
                        rt_kprintf("c1=%d\n",c1);
                    }

                    break;
                default:
                    break;
                }
            }

    }
 }
}

// KEYS线程入口函数
static void keys_thread_entry(void *parameter)
{
    keys_init();
    while(1)
    {
        keys_scan();
        rt_thread_mdelay(10);
    }
}
//创建一个新线程专门用来做LCD显示电流电压
static void LcdPlay_thread_entry(void *parameter)
{
    rt_uint8_t key_flag = 0 ;
    while(1)
    {

        if (c1!=0)
        {
            if (key_flag == 1) {
                LCD_ShowString(3,3, 24*3, 24, 24, "C1", DARKBLUE, BRED);
                key_flag = 0 ;
                c1 = 0 ;
            }
            else if(key_flag == 0){
                LCD_ShowString(3,3, 24*3, 24, 24, "C2", DARKBLUE, BLUE);
                key_flag = 1 ;
                c1 = 0 ;
            }

        }
        if (key_flag == 0) {
            LCD_ShowFloatNum2(50,30,Shunt_Current,6,BLACK,WHITE,16);
            LCD_ShowFloatNum1(50,55,voltageValB/1000,4,BLACK,WHITE,16);
        } else if(key_flag == 1 ){
            LCD_ShowFloatNum2(50,30,Shunt_CurrentB,6,BLACK,WHITE,16);
            LCD_ShowFloatNum1(50,55,voltageValB/1000,4,BLACK,WHITE,16);
        }

        rt_thread_mdelay(50);
    }
}
/* 创建处理线程 */
int bsp_thread_init(void)
{

    rt_thread_t lcdplay;
    /* 创建LED线程 */
    work_thread= rt_thread_create(
                            "work_thread",
                            work_thread_entry,
                            RT_NULL,
                            512,
                            RT_THREAD_PRIORITY_MAX -16,
                            5);
    if (work_thread != RT_NULL) {
        rt_thread_startup(work_thread);
        rt_kprintf("work thread startup\n");
//        return RT_EOK;
    }
    else {
        rt_kprintf("work thread startdown\n");
        return RT_ERROR;
    }

    /*创建keys线程*/
    keys_thread = rt_thread_create("keys_work",
                                   keys_thread_entry,
                                   RT_NULL,
                                   512,
                                   RT_THREAD_PRIORITY_MAX-16,
                                   5);
    if (keys_thread != RT_NULL) {
        rt_thread_startup(keys_thread);
        rt_kprintf("keys thread startup\n");
    }
    else {
        rt_kprintf("keys thread startdown\n");
        return RT_ERROR;
    }

    lcdplay = rt_thread_create("lcdplay",
                           LcdPlay_thread_entry,
                           RT_NULL,
                           2048,
                           RT_THREAD_PRIORITY_MAX -16,
                           20);
    if (lcdplay != RT_NULL) {
        rt_thread_startup(lcdplay);
        rt_kprintf("lcdplay thread startup\n");
    }
    else {
        rt_kprintf("lcdplay thread start fail\n");
        return RT_ERROR;
    }
    return 0;
}

/* 初始化按键线程 */
//INIT_APP_EXPORT(bsp_thread_init);
